package com.example.hotdog.httpu;


import cn.hutool.crypto.symmetric.AES;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.hotdog.entity.Renwu;
import com.example.hotdog.utils.AESUtil;
import com.example.hotdog.utils.MD5Util;
import com.example.hotdog.utils.ObjectToList;
import com.example.hotdog.utils.RSAUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;


public class HttpReHot {
//    public static void main(String[] args) throws Exception {
//
////        String token = "d8ac53cdf5984575bb722d20a3657a1b";
////
//////        List<Map> mapList = postQ(token);
////
////        String cookie = postSave1(token);
////
////        String pin = "286ee9871c1e4f4682d27722a3c51c83" + System.currentTimeMillis() + token +  AESUtil.decryptAES(cookie) ;
////        String md5j = MD5Util.md5(pin);
////        System.out.println("hello");
////
////
////        postSave2(token,cookie,mapList.get(0).get("second_id").toString());
//
//
//    }
//
//    public static void httphots() throws Exception {
////        String token = "d8ac53cdf5984575bb722d20a3657a1b";
////
////        List<Map> mapList = postQ(token);
////
////        String cookie = postSave1(token);
////
////
////        postSave2(token,cookie,mapList.get(0).get("second_id").toString());
//    }



    //支付前1
    public static String postSave1(String token) throws Exception {
        long timestamp = System.currentTimeMillis();

        String body = HttpRequest
                .post("https://api.aichaoliuapp.cn/aiera/sys/device/save")
                .header("Token", token)
                .header("Timestamp", String.valueOf(timestamp))
                .header("Appname", "aiera.sneaker.snkrs.aiera")
                .header("sign", MD5Util.md5(String.valueOf(timestamp) + "5c33494d1b277902d1b78f98093f6fd4"))
                .body("{\n" +
                        "  \"appname\": \"aiera.sneaker.snkrs.aiera\",\n" +
                        "  \"channel\": \"web\",\n" +
                        "  \"appversion\": 1,\n" +
                        "  \"updatetype\": \"new\",\n" +
                        "  \"platform\": \"web\",\n" +
                        "  \"deviceid\": \"web\"\n" +
                        "}")
                .execute().body();
        Map map = JSONObject. parseObject(body, Map.class);
        if ( map.get("cookie") == null){
            return null;
        }
        System.out.println("获取了cookie:" + map.get("cookie").toString());
        return  map.get("cookie").toString();
    }

    //刷新
    public static List<Map> postQ(String token,String projectId,String npId, String price) throws IOException, IllegalAccessException {

        List<Map> mapList1 = new ArrayList<>();

        long timestamp = System.currentTimeMillis();

        String body = HttpRequest
//                .post("https://api.aichaoliuapp.cn/aiera/ai_match_trading/nft_second/sell_order/pay")
                .post("https://api.aichaoliuapp.cn/aiera/ai_match_trading/nft_second/sell_product/list?nft_product_size_id=")
                .header("Token", token)
//                .header("Appname", "aiera.sneaker.snkrs.aiera")
                .header("Timestamp", String.valueOf(timestamp))
//                .header("session", "Vh0glImdNfxym7ag0UMFGuyUILsVjT4bQ5dGqwBolifR2q/tixM5222Rpag3KnbP")
                .header("sign", MD5Util.md5(String.valueOf(timestamp) + "5c33494d1b277902d1b78f98093f6fd4"))

//                .body("{\n" +
//                        "    \"second_id\": 317014910,\n" +
//                        "    \"operate_type\": \"buy\"\n" +
//                        "}")
                .body("{\n" +
                        "    \"product_id\":" + projectId +",\n" +
                        "    \"nft_product_size_id\": " + npId +  ",\n" +
                        "    \"pageNumber\": 1,\n" +
                        "    \"pageSize\": 20,\n" +
                        "    \"unlock\": 1,\n" +
                        "    \"order_by\": \"\",\n" +
                        "    \"prop_pack\": 2\n" +
                        "}")
                .execute().body();
//        JSONObject json= JSON.parseObject(body);
//        System.out.println(body);
        Map map = JSONObject.parseObject(body, Map.class);
        if (!Objects.equals(map.get("code"),0)){
            return mapList1;
        }
        Map map1 = JSONObject.parseObject(map.get("data").toString(), Map.class);
        Object res = map1.get("res");
        List<Map> strings = ObjectToList.objToList(res,Map.class);

        strings.forEach(tempres -> {
            if (Float.parseFloat(tempres.get("price").toString()) <= Float.parseFloat(price)){
//                if (Objects.equals(tempres.get("is_lock"),0)){
//
                mapList1.add(tempres);

//                }
            }

        });

//        System.out.println(System.currentTimeMillis());

        return mapList1;

    }

    //支付前2
    public static void postSave2(String token,String cookie,String secondId,String orderId) throws Exception {
        long timestamp = System.currentTimeMillis();

        HttpRequest request = HttpRequest
                .post("https://api.aichaoliuapp.cn/aiera/ai_match_trading/nft_second/sell_order/pay")
                .header("Token", token)
                .header("session", cookie)
                .header("Timestamp", String.valueOf(timestamp))
                .header("Authorization", MD5Util.md5("286ee9871c1e4f4682d27722a3c51c83" + timestamp + token +  AESUtil.decryptAES(cookie)))
//                .header("sign", MD5Util.md5(String.valueOf(timestamp) + "5c33494d1b277902d1b78f98093f6fd4"))
                .body("{\n" +
                        "    \"second_id\": "+ secondId + ",\n" +
                        "    \"operate_type\": \"buy\"\n" +
                        "}");

        String body = request.execute().body();


        System.out.println("支付前2" + body);
        Map map = JSONObject.parseObject(body, Map.class);
        Map data = JSONObject.parseObject(map.get("data").toString(), Map.class);
        Object code = map.get("code");

        while (Objects.equals(90063,code)){
            String s = postSave1(token);
            if ( s != null){
                postSave2(token,s,secondId,orderId);
            }else {
                break;
            }

        }

//        if (Objects.equals("10151",code)){
//            //TODO 已出售
//        }



        if (Objects.equals(0,code)){

            System.out.println("锁单了");
//            System.out.println("TOKEN:"+token);
//            System.out.println("orderId:"+data.get("order_id"));
            String order_no = postZPre(token, data.get("order_id").toString());
//            System.out.println("order_no:"+order_no);
//            if (map.get("data") != null){
//                Map data = JSONObject.parseObject(body, Map.class);
//                if (data.get("order_id") != null){
//                    postZ(token,data.get("order_id").toString(),cookie);
//                }
//            }
//            if (order_no != null){
//                postZ(token,order_no,cookie);
//            }

            //TODO 去支付
//            String rrr = "";
//            File file = new File("./dataf/success.txt");
//            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file,true));
//            bufferedWriter.write(rrr);
//            bufferedWriter.newLine();
//            bufferedWriter.flush();

        }



    }

    //支付pre
    public static String postZPre(String token,String orderId) throws Exception {


        long timestamp = System.currentTimeMillis();

        String body = HttpRequest
                .post("https://api.aichaoliuapp.cn/aiera/v2/hotdog/order/prepay")
                .header("Token", token)
//                .header("Appname", "aiera.sneaker.snkrs.aiera")
                .header("Timestamp", String.valueOf(timestamp))
//                .header("session", "Vh0glImdNfxym7ag0UMFGuyUILsVjT4bQ5dGqwBolifR2q/tixM5222Rpag3KnbP")
                .header("sign", MD5Util.md5(String.valueOf(timestamp) + "5c33494d1b277902d1b78f98093f6fd4"))

//                .body("{\n" +
//                        "    \"second_id\": 317014910,\n" +
//                        "    \"operate_type\": \"buy\"\n" +
//                        "}")
                .body("{\n" +
                        "  \"order_id\": "+ orderId +",\n" +
                        "  \"pay_channel\": 4\n" +
                        "}")
                .execute().body();
//        JSONObject json= JSON.parseObject(body);
        System.out.println("订单协议:" + body);
        Map map = JSONObject.parseObject(body, Map.class);
        if (Objects.equals(map.get("code"),0)){
            Map reque = JSONObject.parseObject(map.get("data").toString(), Map.class);
            postZ(token,reque.get("order_no").toString());
            return reque.get("order_no").toString();
        }

        return null;

    }

    //支付
    public static void postZ(String token,String orderNo) throws Exception {
        System.out.println("orderNo"+orderNo);

        String body = HttpRequest
                .post("https://api.aichaoliuapp.cn/aiera/v2/hotdog/payment/kft/confirm")
                .header("Token", token)
//                .header("Appname", "aiera.sneaker.snkrs.aiera")
//                .header("Timestamp", String.valueOf(timestamp))
//                .header("session", "Vh0glImdNfxym7ag0UMFGuyUILsVjT4bQ5dGqwBolifR2q/tixM5222Rpag3KnbP")
//                .header("sign", MD5Util.md5(String.valueOf(timestamp) + "5c33494d1b277902d1b78f98093f6fd4"))
//                .header("Authorization", MD5Util.md5("286ee9871c1e4f4682d27722a3c51c83" + timestamp + token +  AESUtil.decryptAES(cookie)))

//                .body("{\n" +
//                        "    \"second_id\": 317014910,\n" +
//                        "    \"operate_type\": \"buy\"\n" +
//                        "}")
                .body("{\n" +
                        "    \"order_no\": \""+ orderNo  + "\",\n" +
                        "    \"pay_channel\": 4,\n" +
                        "    \"confirm_flag\": \"1\",\n" +
                        "    \"pay_pwd\": \" " + RSAUtils.publicKeyEncrypt("1145") + " \"\n" +
                        "}")
                .execute().body();
//        JSONObject json= JSON.parseObject(body);
        System.out.println("支付"+body);




    }
}
